You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

43 lines
1.6 KiB

import { delCache } from "#server/utils/context";
import { z } from "zod";
import { validate } from "#server/utils/validation";
import { updateCard, getCardById, CardTypes } from "../../service/card";
const updateSchema = z.object({
type: z.enum(CardTypes).optional(),
title: z.string().min(1, "标题不能为空").max(60, "标题不能超过 60 字").optional(),
description: z.string().max(200, "描述不能超过 200 字").nullable().optional(),
content: z.string().max(50000, "正文不能超过 50000 字").nullable().optional(),
aspectRatio: z.number().min(0).max(99).nullable().optional(),
categoryId: z.string().nullable().optional(),
images: z
.array(
z.object({
url: z.string().min(1, "图片地址不能为空").max(500),
sortOrder: z.number().int().optional(),
}),
)
.nullable()
.optional(),
tagIds: z.array(z.number().int().positive()).nullable().optional(),
articleIds: z.array(z.number().int().positive()).nullable().optional(),
});
export default defineWrappedResponseHandler(async (event) => {
const idParam = getRouterParam(event, "id");
if (!idParam) return R.throwError(400, "缺少卡片 ID", null);
const id = parseInt(idParam);
if (isNaN(id)) return R.throwError(400, "卡片 ID 格式不正确", null);
const body = await readBody(event);
const data = validate(updateSchema, body);
const existing = await getCardById(id);
if (!existing) return R.throwError(404, "卡片不存在", null);
const updated = await updateCard(id, data);
await delCache(`card:${id}`);
await delCache("categories:tree");
return R.success(updated);
});